﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WPFFolderBrowser;
using System.Windows.Forms.Integration;
using AxShockwaveFlashObjects;
using mshtml;
using ScormModel;
using System.IO;
namespace WpfELearningAuthoringTool
{
    /// <summary>
    /// Interaction logic for InsertFlashDialog.xaml
    /// </summary>
    public partial class InsertFlashDialog : Window
    {
        private IHTMLDocument2 doc;
        string currentDirectory;
        Resource re;
        public InsertFlashDialog()
        {
            InitializeComponent();
        }
        public IHTMLDocument2 DocManager
        {
            get { return this.doc; }
            set { this.doc = value; }
        }
        public Resource SelectResource
        {
            get { return this.re; }
            set { this.re = value; }
        }
        public string CurrentDirectory
        {
            get { return this.currentDirectory; }
            set { this.currentDirectory = value; }
        }
        private void btnBrowse_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
                dlg.DefaultExt = ".swf";
                dlg.Filter = "Media files(.swf)|*.swf";
                Nullable<bool> result = dlg.ShowDialog();
                if (result == true)
                {
                    txtFlashPath.Text = dlg.FileName;              
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnPreview_Click(object sender, RoutedEventArgs e)
        {
            if (txtFlashPath.Text.Length == 0)
            {
                MessageBox.Show("Please select flash file");
                return;
            }

            try
            {
                if (txtFlashPath.Text.Contains(".swf"))
                {
                    flashView.Children.Clear();
                    string strFilePath = txtFlashPath.Text;
                    SWFFileHeader swfFile = new SWFFileHeader(strFilePath);


                    WindowsFormsHost host = new WindowsFormsHost();
                    FormFlashLibrary.FlashAxControl player = new FormFlashLibrary.FlashAxControl();

                    //the Windows Forms Host hosts the Flash Player
                    host.Child = player;

                    //the WPF Grid hosts the Windows Forms Host
                    flashView.Children.Add(host);

                    //set size
                    player.Width = Convert.ToInt32(txtFlashWidth.Text);
                    player.Height = Convert.ToInt32(txtFlashHeight.Text);

                    //load & play the movie
                    player.LoadMovie(strFilePath);
                    player.Play();
                }
                else if (txtFlashPath.Text.Contains(".mp4"))
                {
                    flashView.Children.Clear();
                    string strFilePath = txtFlashPath.Text;
                    MediaElement me = new MediaElement();
                    flashView.Children.Add(me);
                    me.Source = new Uri(strFilePath);
                    me.Play();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
           
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!File.Exists(currentDirectory + "/media/" + System.IO.Path.GetFileName(txtFlashPath.Text)))
                {
                    File.Copy(txtFlashPath.Text, currentDirectory + "/media/" + System.IO.Path.GetFileName(txtFlashPath.Text));
                }
                string flashScript = MediaUtil.buildFlashScript(txtFlashPath.Text, txtFlashWidth.Text, txtFlashHeight.Text);
                var element = doc.createElement("div");
                element.innerHTML = flashScript;
                doc.body.insertAdjacentHTML("beforeEnd", element.outerHTML);
                //check duplicate resource
                bool test = false;
                for (int i = 0; i < re.ListFileRef.Count; i++)
                {
                    if (re.ListFileRef[i].Bref.Contains(System.IO.Path.GetFileName(txtFlashPath.Text)))
                        test = true;
                    else
                        continue;
                }
                if (test == false)
                {
                    FileRef fr = new FileRef();
                    fr.Bref = "media/"+System.IO.Path.GetFileName(txtFlashPath.Text);
                    re.ListFileRef.Add(fr);
                }
               // MessageBox.Show("create flash success");
                Close();
            }
            catch (Exception ex) {
                MessageBox.Show("create flash fail");
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}
